package com.example.covid19.service.impl;

import com.example.covid19.common.Dto.goodsDto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Goods;
import com.example.covid19.entity.Picp;
import com.example.covid19.mapper.GoodsMapper;
import com.example.covid19.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.covid19.service.PicpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;

import static com.google.common.io.Files.getFileExtension;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BaiZhengChun
 * @since 2022-09-12
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    private final GoodsMapper goodsMapper;
    private final PicpService picpService;

    public GoodsServiceImpl(GoodsMapper goodsMapper, PicpService picpService) {
        this.goodsMapper = goodsMapper;
        this.picpService = picpService;
    }


    @Override
    public String getbase(String picid) throws FileNotFoundException {
        File file = new File(picid);
        String name = getFileExtension(String.valueOf(file));
        InputStream in = new FileInputStream(file);
        byte[] data = null;
        try {
//                in = file.openStream();//远程文件
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Base64.Encoder encoder = Base64.getEncoder();
        String code = "data:Image/" + name + ";base64," + encoder.encodeToString(data);
        return code;
    }
    @Override
    public Result goodsinfo(Integer pageNo,Integer pagesize) throws FileNotFoundException {
        HashMap<Object,Object> map = new HashMap<>(2);
        int k = goodsMapper.selectCount(null);
        int i = goodsMapper.gooinfo3(pageNo,pagesize).size();
        List<goodsDto> list = goodsMapper.gooinfo3(pageNo,pagesize);
        map.put("count",k);
        map.put("info",list);
        return Result.success(200, "查询成功", map);
    }
    @Override
    public Result editgoods(goodsDto goodsDto) {
        System.out.println(goodsDto);
        Goods goods = new Goods();
        goods.setType(goodsDto.getType());
        goods.setQuantity(goodsDto.getQuantity());
        goods.setBrand(goodsDto.getBrand());
        goods.setPrice(goodsDto.getPrice());
        goods.setProducingArea(goodsDto.getProducingArea());
        goods.setQuality(goodsDto.getQuality());
        goods.setGoodsname(goodsDto.getGoodsname());
        goods.setSid(goodsDto.getSid());
        Picp picp = new Picp();
        boolean flag2 = this.updateById(goods);
        if (flag2){
            return Result.success(200,"成功",null);
        }else {
            return Result.fail(400,"失败",null);
        }


    }
    @Override
    public Result edpic(MultipartFile multipartFile,String str2){
        String str = upLoad(multipartFile);
        Picp picp = new Picp();
        picp.setPicpath(str);
        picp.setPicid(str2);
        System.out.println(picp);
        boolean flag = picpService.updateById(picp);
        if (flag){
            return Result.success(200,"成功",null);
        } else {
//            System.out.println("fewsfwe");
            return Result.success(400,"faf",null);
        }

    }
    @Override
    public String upLoad(MultipartFile multipartFile){
        log.info("文件上传开始");
        log.info("文件{}",multipartFile.getOriginalFilename());
        String str;
        if (!multipartFile.isEmpty()){
            try {
                //上传的文件需要保存的路径和文件名称，路径需要存在，否则报错
                multipartFile.transferTo(new File("F:/GQCresp/"+multipartFile.getOriginalFilename()));
                str="F:/GQCresp/"+multipartFile.getOriginalFilename();

            } catch (IllegalStateException | IOException e){
                e.printStackTrace();
                return "上传失败";
            }
        } else {
            return "请上传文件";
        }
        return str;
    }



}
